{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Basic buildling blocks\n",
    "\n",
    "Get to know the basic building blocks of Superlinked.\n",
    "\n",
    "1. Describe your data using Python classes with the [@schema](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/common/schema/schema.md) decorator.\n",
    "2. Describe your vector embeddings with [Spaces](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/dsl/space/index.md).\n",
    "3. Combine your embeddings into a queryable [Index](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/dsl/index/index.m.md).\n",
    "4. Define your search with dynamic parameters and weights as a [Query](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/dsl/query/query.md).\n",
    "5. Load your data using a [Source](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/dsl/source/index.md).\n",
    "6. Run your configuration with an [Executor](https://github.com/superlinked/superlinked-alpha/blob/main/docs/superlinked/framework/dsl/executor/in_memory/in_memory_executor.md)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install superlinked==3.38.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from superlinked.framework.common.schema.schema import schema\n",
    "from superlinked.framework.common.schema.schema_object import String\n",
    "from superlinked.framework.common.schema.id_schema_object import IdField\n",
    "from superlinked.framework.dsl.space.text_similarity_space import TextSimilaritySpace\n",
    "from superlinked.framework.dsl.index.index import Index\n",
    "from superlinked.framework.dsl.query.param import Param\n",
    "from superlinked.framework.dsl.query.query import Query\n",
    "from superlinked.framework.dsl.source.in_memory_source import InMemorySource\n",
    "from superlinked.framework.dsl.executor.in_memory.in_memory_executor import (\n",
    "    InMemoryExecutor,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a schema for your data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@schema\n",
    "class ParagraphSchema:\n",
    "    body: String\n",
    "    id: IdField"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instantiate a new instance of your schema to start the pipeline definition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph = ParagraphSchema()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a space that will run a transformers model on the body of the paragraph."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "relevance_space = TextSimilaritySpace(text=paragraph.body, model=\"all-MiniLM-L6-v2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Group your space in an index to make it retrievable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "paragraph_index = Index(relevance_space)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Define a query that will search for similar paragraphs in the index. The parameters will be filled later on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "query = (\n",
    "    Query(paragraph_index)\n",
    "    .find(paragraph)\n",
    "    .similar(relevance_space.text, Param(\"query_text\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an in-memory source and executor to try out your configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "source: InMemorySource = InMemorySource(paragraph)\n",
    "executor = InMemoryExecutor(sources=[source], indices=[paragraph_index])\n",
    "app = executor.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Insert some example data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "source.put([{\"id\": \"happy_dog\", \"body\": \"That is a happy dog\"}])\n",
    "source.put([{\"id\": \"happy_person\", \"body\": \"That is a very happy person\"}])\n",
    "source.put([{\"id\": \"sunny_day\", \"body\": \"Today is a sunny day\"}])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Query your data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>That is a very happy person</td>\n",
       "      <td>happy_person</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>That is a happy dog</td>\n",
       "      <td>happy_dog</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Today is a sunny day</td>\n",
       "      <td>sunny_day</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          body            id\n",
       "0  That is a very happy person  happy_person\n",
       "1          That is a happy dog     happy_dog\n",
       "2         Today is a sunny day     sunny_day"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = app.query(query, query_text=\"This is a happy person\")\n",
    "\n",
    "result.to_pandas()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check how a different query can produce different results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>body</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>That is a happy dog</td>\n",
       "      <td>happy_dog</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>That is a very happy person</td>\n",
       "      <td>happy_person</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Today is a sunny day</td>\n",
       "      <td>sunny_day</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          body            id\n",
       "0          That is a happy dog     happy_dog\n",
       "1  That is a very happy person  happy_person\n",
       "2         Today is a sunny day     sunny_day"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = app.query(query, query_text=\"This is a happy dog\")\n",
    "\n",
    "result.to_pandas()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
